Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sync-rpc

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sync-rpc

Run asynchronous commands synchronously by putting them in a separate process

  • 1.3.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
913K
decreased by-21.83%
Maintainers
1
Weekly downloads
 
Created

What is sync-rpc?

The sync-rpc npm package allows for synchronous remote procedure calls (RPC) in Node.js. It enables the execution of functions in a separate process and returns the result synchronously, which can be useful for certain use cases where asynchronous behavior is not desired.

What are sync-rpc's main functionalities?

Synchronous RPC Call

This feature allows you to make synchronous RPC calls to a worker script. In this example, the worker script (worker.js) contains a function to add two numbers. The main script calls this function synchronously and logs the result.

const syncRpc = require('sync-rpc');
const rpc = syncRpc(__dirname + '/worker.js');
const result = rpc({ method: 'add', params: [1, 2] });
console.log(result); // Outputs: 3

Handling Multiple RPC Methods

This feature demonstrates how to handle multiple RPC methods within the worker script. The main script can call different methods (e.g., 'add' and 'subtract') and receive the results synchronously.

const syncRpc = require('sync-rpc');
const rpc = syncRpc(__dirname + '/worker.js');
const addResult = rpc({ method: 'add', params: [1, 2] });
const subtractResult = rpc({ method: 'subtract', params: [5, 3] });
console.log(addResult); // Outputs: 3
console.log(subtractResult); // Outputs: 2

Error Handling in RPC

This feature shows how to handle errors in synchronous RPC calls. If the worker script throws an error (e.g., division by zero), the main script can catch and handle the error appropriately.

const syncRpc = require('sync-rpc');
const rpc = syncRpc(__dirname + '/worker.js');
try {
  const result = rpc({ method: 'divide', params: [1, 0] });
  console.log(result);
} catch (error) {
  console.error('Error:', error.message);
}

Other packages similar to sync-rpc

FAQs

Package last updated on 24 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc